const ora = require('ora');
async function sleep(n) {
    return new Promise((resolve,rejected)=>setTimeout(resolve,n));
}

async function wrapLoading(fn, message, ...args) {
    const spinner = ora(message);
    spinner.start();
    try {
        let res = await fn(...args)
        spinner.succeed(); // 加载成功
        return res;
    }catch(e) {
        console.log(e,"\r\n");
        spinner.fail('request failed, refetch ...');
        await sleep(1000);
        return wrapLoading(fn,message,...args)
    }
}

module.exports = {
    sleep,
    wrapLoading
}